// middlewares/asyncHandler.js
const { validationResult } = require('express-validator');
const asyncHandler = (fn) => async (req, res) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
    }
    try {
        const result = await fn(req);
        res.status(result.code).json(result);
    } catch (error) {
        res.status(error.code).json(error);
    }
};

module.exports = asyncHandler;